#发送请求
import json

import requests

ip = "http://192.168.184.137:8080"

loginData={"username":"admin","password":"123456"}

r=requests.post(f"{ip}/login",data=loginData)

print(r)

print(r.json())
#获取token
token  = r.json()['Admin-Token']

# header={'Admin-Token':token}

#上传文件
fData={'type':'img'}
fname=r'C:\Users\32086\Desktop\用例\123.jpg'
fData2={'file':('123.jpg',open(fname,'rb'),'image/jpeg')}

r2 = requests.post(f'{ip}/file/upload',data=fData,files=fData2,headers={'Admin-Token':token})

print(r2)
print(r2.json())

#获取batchId
ebatchId = r2.json()['batchId']

#增加日志
getLogData = {"categoryId":"1","content":"123","tomorrow":"132","question":"321","batchId":ebatchId,"sendUserIds":"","sendDeptIds":"","customerIds":"","contactsIds":"","businessIds":"","contractIds":""}

r3 =requests.post(f'{ip}/OaLog/addOrUpdate',json=getLogData,headers={'Admin-Token':token})

print(r3)
print(r3.json())

#查看日志
getLogData = {'page':1,'limit':5,'by':1}

r =requests.post(f'{ip}/OaLog/queryList',json=getLogData,headers={'Admin-Token':token})

print(r)
print(r.json())

#获取logId
b_aId = r.json()['data']['list'][0]['logId']
print(b_aId)

#修改日志
updateLogData = {"logId":b_aId,"categoryId":"1","content":"更改内容1","tomorrow":"更改内容2","question":"更改内容3","batchId":ebatchId,"sendUserIds":"","sendDeptIds":"","customerIds":"","contactsIds":"","businessIds":"","contractIds":""}

r4 =requests.post(f'{ip}/OaLog/addOrUpdate',json=updateLogData,headers={'Admin-Token':token})

print(r4)
print(r4.json())

#
# # 删除日志
delLogData = {"logId":b_aId}

r5 =requests.post(f'{ip}/OaLog/deleteById',data=delLogData,headers={'Admin-Token':token})

print(r5)
print(r5.json())


if __name__ == '__main__':
    #字典与json字符串相互转换
    header = {'Admin-Token':'token'}
    print(header,type(header))
    #字典转换成字符串
    l=json.dumps(header)
    print(l,type(l))
    #字符串转换为字典
    d=json.loads(l)
    print(d,type(d))

    # 字典与json文件相互转换
    header = {'Admin-Token':'token'}
    print(header,type(header))
    # 字典转换成json文件
    fname = r'C:\python\2504\apiTest\datas\myjson.json'
    f = open(fname,'w')
    json.dump(header,f)

    # json文件转换为字典
    fname = r'C:\python\2504\apiTest\datas\myjson.json'
    d = open(fname,'r')
    dd = json.load(d)
    print(dd,type(dd))
